import urllib.request
import http.cookiejar
import sys
import getpass
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import re

url = "http://e.ustb.edu.cn/userPasswordValidate.portal"#登陆url
data={}#表单
data['Login.Token1']=input("账号")
data['Login.Token2']=getpass.getpass("密码")
data['goto']=r'http://e.ustb.edu.cn/loginSuccess.portal'
data['gotoOnFail']=r'http://e.ustb.edu.cn/loginFailure.portal'
headers = {'User-Agent': r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
			r'Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3'}

#使用账号密码登录
def loginByPass(data,url,headers):
	data = urllib.parse.urlencode(data)
	data = data.encode('utf-8')
	req = urllib.request.Request(url,data,headers=headers)
	response = urllib.request.urlopen(req)
	html = response.read()
	if 'Successed' in str(html):
		print('----------------')
		print('Login Successed!|')
		print('----------------')
	return req

#获取并保存cookie
def saveCookie(req):
	cj = http.cookiejar.CookieJar()
	opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
	r = opener.open(req)
	# print(r.read().decode('utf-8'))
	#打印cookie
	# for item in cj:
	# 	print('Name = %s' % item.name)
	# 	print('Value = %s' % item.value)
	return cj

# 利用cookie登陆返回余额
def loginByCookie(cj):
	handler = urllib.request.HTTPCookieProcessor(cj)
	opener = urllib.request.build_opener(handler)
	get_url = 'http://e.ustb.edu.cn/index.portal'  # 利用cookie请求訪问还有一个网址
	get_request = urllib.request.Request(get_url)
	get_response = opener.open(get_request)
	htmlmessage = get_response.read().decode('utf-8')
	money = re.search('[\d]{2,3}'+'.'+'[\d]{2}'+r'</font>',htmlmessage)
	money = re.search('[\d]{2,3}'+'.'+'[\d]{2}',str(money[0]))
	return money[0]

def bill(cj):
	handler = urllib.request.HTTPCookieProcessor(cj)
	opener = urllib.request.build_opener(handler)
	get_url = 'http://e.ustb.edu.cn/index.portal?.pn=p378_p381'  # 利用cookie请求訪问还有一个网址
	get_request = urllib.request.Request(get_url)
	get_response = opener.open(get_request)
	htmlmessage = get_response.read().decode('utf-8').replace(' ', '').replace('\t', '').replace('\r\n\r\n', '')
	money = re.search(r'<tdalign="center">(.\n*)*?</table>', htmlmessage)

	cost = re.findall(r'class="table_text">((.*)*?)</td>', htmlmessage)
	i = 1
	while i < 37:
		print(cost[i][0])
		if(i % 4 == 0 ):
			print('-------------------')
		i = i + 1

def main():
	money = loginByCookie(saveCookie(loginByPass(data,url,headers)))
	bill(saveCookie(loginByPass(data, url, headers)))
	return money

class Show(QWidget):
    def __init__(self, parent = None):
        super(Show, self).__init__(parent)
        self.resize(200, 100)
        self.setWindowTitle("余额")

        self.lcd = QLCDNumber()
        self.lcd.setDigitCount(8)
        self.lcd.setMode(QLCDNumber.Dec)
        self.lcd.setSegmentStyle(QLCDNumber.Flat)
        self.lcd.display(Mymoney)

        layout = QVBoxLayout()
        layout.addWidget(self.lcd)
        self.setLayout(layout)

Mymoney = main()
app = QApplication(sys.argv)
t = Show()
t.show()
sys.exit(app.exec_())
